# python -m pip install pyserial

import serial
import time

# 大华电子秤协议

ser = serial.Serial("COM6", 9600, timeout=1)
ser.flushInput()


def main():
    while True:
        count = ser.inWaiting()
        if count != 0:
            recv = ser.read(ser.in_waiting).decode("utf-8")
            checktime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

            splitres = (recv.splitlines())

            if len(splitres) != 3:
                continue
            if splitres[2].strip() == '':
                continue

            print(checktime, " ---  recv --> ", dahuadeal(splitres[2]))

        time.sleep(0.1)  #


def dahuadeal(indahuaread):
    dahuares = str(int(indahuaread) / 1000) + 'kg'
    return dahuares


if __name__ == '__main__':
    main()
